import { BodySetup } from "utils/creep/CreepSetup";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { EActionCode } from "framework/FSM/Action/ActionCode";

import { cpu_used } from "utils/decorate/decorate";
/**
 * 升级Action
 */
/*@profile*/
export class ActionTransfer  extends ActionBaseOfCreep<IActionCMD<"transfer">>
{
    @cpu_used((action:any)=>{
        return `${action.arg.entity.GUID()}-${action.key}-updateArgs`
    },
    (action:any,cpu:number)=>
    {
        return action.arg.entity.entityType()=="Creep" && cpu>1
    }
    )
    protected updateArgs(action: IActionCMD<"transfer">): EActionCode 
    {
        if(action.isExpired && action.isExpired(action.arg))
        {

        }
        if(super.updateArgs(action) !== EActionCode.OK)
        {
            return EActionCode.FAILED;
        }
        if(action.arg.resource)
        {
            if(action.arg.resource.length ==0)
            {
                return EActionCode.FAILED;
            }
        }
        else 
        {
            // 爬空了 
            if(action.arg.entity.store.getUsedCapacity()==0)
            {
                return EActionCode.FAILED;
            }
        }
        action.arg.store = Game.getObjectById(action.arg.store.id) as AnyStoreStructure;
        if(!action.arg.store)
        {
            return EActionCode.FAILED;
        }
        
        return EActionCode.OK;
    }
    @cpu_used((action:any)=>{
        return `${action.arg.entity.GUID()}-${action.key}-_execute`
    },
    (action:any,cpu:number)=>
    {
        return action.arg.entity.entityType()=="Creep" && cpu>0.5
    }
    )
    protected _execute(action: IActionCMD<"transfer">): ActionCode 
    {
        const structure = action.arg.store;
        const creep = action.arg.entity;
        // if(creep.ActiveRole() == "carry")
        // {
        //     creep.log(`warn`,`准备填充`)
        // }
        if(structure.store.getFreeCapacity(RESOURCE_ENERGY)==0)
        {
            
            creep.log(`warn`,`准备填充`);
            return EActionCode.OK;
        }
        // global.Log.Info(`ActionTransfer ${creep.name}`);
        if(structure.pos.distance(creep.pos)>1)
        {
            if(structure.store.getFreeCapacity(RESOURCE_ENERGY)==0)
            {
                if(1 == creep.mymoveTo({pos:structure.pos,range:5,rangelimit:5},{}))
                {
                    return EActionCode.OK;
                }
            }
            else
            {
                if(1 == creep.mymoveTo({pos:structure.pos,range:1,rangelimit:5},{}))
                {
                    return EActionCode.OK;
                }
            }
            
        }
        else
        {
            if(action.arg.resource)
            {
                // 转移指定资源
                for(let i=0;i<action.arg.resource.length;i++)
                {
                    const e=action.arg.resource[i];
                    if(e.amount)
                    {
                        creep.transfer(structure,e.resourceType,e.amount);
                        action.arg.resource.splice(i,1);
                        return EActionCode.RUNNING;
                    }
                    else
                    {
                        creep.transfer(structure,e.resourceType  );
                        action.arg.resource.splice(i,1);
                        return EActionCode.RUNNING;
                    }
                }
            }
            else
            {
                // 全部转移
                for(const k in creep.store)
                {
                    if(OK == creep.transfer(structure,k as ResourceConstant))
                    {
                        return EActionCode.RUNNING;
                    }
                }
            }
            
        }
        return EActionCode.RUNNING;
    }
}